package com.yunhoo.cqq.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class StrUtil {

    /**
     * if (str==null){
     * return "";
     * }
     * @param str
     * @return
     */
    public static String val(String str) {
        if (str == null || "".equals(str)) {
            return "";
        } else {
            return str.trim();
        }
    }

    /**
     * if((Integer)flag!=null && flag!=0) return true;
     */
    public static boolean isInt(Integer flag) {
        if (flag != null && flag != 0) {
            return true;
        }
        return false;
    }

    /**
     * if(list!=null && list.size()>0)
     * return true;
     * @param str
     * @param match
     * @return
     */
    public static boolean listEmp(List list) {

        if (list != null && list.size() > 0) {
            return true;
        }
        return false;
    }

    public static int gettimes(String str, Character match) {
        int cnt = 0, start = 0;
        int flag = 0;
        for (int i = 0; i < str.length(); i++) {
            if (match.equals(str.charAt(i))) {
                flag++;
            }
        }
        return flag;
    }

    public static int gettimes(String str, String match) {
        int cnt = 0, start = 0;
        int flag = 0;
        for (int i = 0; i < str.length() - match.length() + 1; i++) {
            if (match.equals(str.substring(i, i + match.length()))) {
                flag++;
            }
        }
        return flag;
    }

    public static String trans(String str) {
        String strs = "";
        try {
            strs = new String(str.getBytes("iso8859-1"), "utf-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return strs;
    }

    /**
     * if(flag!=null && !"".equals(flag)) return true;
     * @param flag
     * @return
     */
    public static boolean isEmpty(String flag) {
        if (flag != null && !"".equals(flag)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     *  if (flag == null || "".equals(flag))
     * @param flag
     * @return
     */
    public static boolean empty(String flag) {
        if (flag == null || "".equals(flag.trim())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * �if(strs[i]!=null && !"".equals(strs[i]) ) return true;
     * @param strs
     * @return
     */
    public static boolean isEmpty(String... strs) {
        boolean flag = false;
        int j = 0;
        for (int i = 0; i < strs.length; i++) {
            if (strs[i] != null && !"".equals(strs[i].trim())) {
                j++;
            }
        }
        if (j == strs.length) {
            flag = true;
        }
        return flag;
    }

    public static String[] dealArray(String[] strs) {
        String[] infos = new String[strs.length];
        for (int i = 0; i < strs.length; i++) {
            String str = strs[i];
            String concer = StrUtil.trans(str);
            infos[i] = concer.trim();
        }
        return infos;
    }

    public static String[] dealString(String str, String reg) {
        if (isEmpty(str)) {
            String[] strs = str.split(reg);
            return strs;
        }
        return null;
    }

    /**
     * �пյĴ���
     * @param strs
     * @return
     */
    public static boolean isAllEmpty(String... strs) {
        int lenth = strs.length;
        int i = 0;
        for (String str : strs) {
            if (str == null || "".equals(str)) {
                i++;
            }
        }
        if (i == lenth) {
            return true;
        }
        return false;
    }

    /**
     * 比较两字符串数组的值是否完全一样
     * @param args
     */
    public static boolean compareArray(String[] first, String[] second) {
        int flag = 0;
        if (first != null && second != null) {
            if (first.length == second.length) {
                for (int i = 0; i < first.length; i++) {
                    if (trim(first[i]).equals(trim(second[i]))) {
                        // System.out.println(first[i]+" "+second[i]);
                        flag++;
                    }
                }
                if (flag == first.length) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     *
     * @param args
     */
    public static String getRandomString(String fileName) {
        if (fileName.lastIndexOf(".") != -1) {
            return UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
        } else {
            return UUID.randomUUID().toString() + fileName;
        }
    }

    /**
     * yj1-yd1+yj2-yd2+.....
     * @param startmonth
     * @param lastmonth
     * @return
     */
    public static String queryStringForMonth(String month) {
        String flag = "";
        if (month != null) {
            Integer mont = Integer.parseInt(month);
            flag += "yj" + mont + "-" + "yd" + mont;
        }
        return flag;
    }

    /**
     * yj1-yd1+yj2-yd2+.....
     * @param startmonth
     * @param lastmonth
     * @return
     */
    public static String queryString(String startmonth, String lastmonth) {
        String flag = "";
        if (startmonth != null && lastmonth != null) {
            Integer startm = Integer.parseInt(startmonth);
            Integer lastm = Integer.parseInt(lastmonth);
            if (startm <= lastm) {
                Integer mont = lastm - startm + 1;
                for (int i = startm; i <= lastm; i++) {
                    if (i == lastm) {
                        flag += "yj" + i + "-" + "yd" + i;
                    } else {
                        flag += "yj" + i + "-" + "yd" + i + "+";
                    }
                }
            }
        }
        return flag;
    }

    /**
     * yj1 - yd1
     *@param currentMonth
     * @return
    */
    public static String queryCurrentMonthStr(String currMonth) {
        String flag = "";
        if (currMonth != null) {
            Integer lastm = Integer.parseInt(currMonth);
            flag += "yj" + lastm + "-" + "yd" + lastm;
        }
        return flag;
    }
    /**
     * yj1-yd1+yj2-yd2+.....
     * @param startmonth
     * @param lastmonth
     * @return
     */
    public static String queryNcStr(String lastmonth) {
        //String flag = "ncj-ncd+";
        String flag = "";
        if (lastmonth != null) {
            Integer lastm = Integer.parseInt(lastmonth);
            if (1 <= lastm) {
                for (int i = 1; i <= lastm; i++) {
                    if (i == lastm) {
                        flag += "yj" + i + "-" + "yd" + i;
                    } else {
                        flag += "yj" + i + "-" + "yd" + i + "+";
                    }
                }
            }
        }
        return flag;
    }

    /**
     * yj1+yj2+....
     */
    public static String queryTempString(String startmonth, String lastmonth) {
        String flag = "";
        if (startmonth != null && lastmonth != null) {
            Integer startm = Integer.parseInt(startmonth);
            Integer lastm = Integer.parseInt(lastmonth);
            if (startm <= lastm) {

                for (int i = startm; i <= lastm; i++) {

                    flag += "yj" + i + "+";

                }
            }
        }
        String reStr = "";
        if (isEmpty(flag)) {
            reStr = flag.substring(0, flag.length() - 1);
        }
        return reStr;
    }
    public static String queryTempStringd(String startmonth, String lastmonth) {
        String flag = "";
        if (startmonth != null && lastmonth != null) {
            Integer startm = Integer.parseInt(startmonth);
            Integer lastm = Integer.parseInt(lastmonth);
            if (startm <= lastm) {

                for (int i = startm; i <= lastm; i++) {

                    flag += "yd" + i + "+";

                }
            }
        }
        String reStr = "";
        if (isEmpty(flag)) {
            reStr = flag.substring(0, flag.length() - 1);
        }
        return reStr;
    }


    /**
     * "3333,33333" to '3333','3333'
     */
    public static String getSqlString(String str) {
        String string = "";
        if (isEmpty(str)) {
            if (str.contains(",")) {
                String[] strs = str.split(",");
                for (int i = 0; i < strs.length; i++) {
                    if (i == strs.length - 1) {
                        string += "'" + strs[i].trim() + "'";
                    } else {
                        string += "'" + strs[i].trim() + "',";
                    }
                }
            } else {
                string = "'" + str.trim() + "'";
            }
        } else {
            string = "''";
        }
        return string;
    }

    public static String trim(String str) {
        if (str != null) {
            return str.replaceAll(" ", "");
        }
        return str;
    }

    /**
     * 操作下面字符串
     * [028002]一般行政管理事务现金,[051002]一般行政管理事务限额
     * 变成 list 放着028002 051002
     */
    public static List<String> transforStr(String str) {
        List list = new ArrayList();
        if (str.contains(",")) {
            String[] strs = str.split(",");
            for (int i = 0; i < strs.length; i++) {
                String strl = strs[i];
                if (strl.contains("[") && strl.contains("]")) {
                    String stri = strl.substring(strl.indexOf("[") + 1, strl.indexOf("]"));
                    list.add(stri);
                }
            }
        } else {
            if (str.contains("[") && str.contains("]")) {
                String stri1 = str.substring(str.indexOf("[") + 1, str.indexOf("]"));
                list.add(stri1);
            }
        }
        return list;
    }

    /**
     * 生成需要的月份
     */
    public static String matchMonthString(String flag) {
        String str = "";
        String strmonth = "";
        for (int i = 1; i < 14; i++) {
            str += flag + i + "+";
        }
        if (isEmpty(str)) {
            strmonth = str.substring(0, str.length() - 1);
        }
        return strmonth;

    }

    /**
     * 生成需要的月份
     */
    public static String matchMonthString(String first, String second, String endmonth) {
        String str = "";
        if (first.equals("yj")) {
            str = "ncj-ncd+";
        } else if (first.equals("yd")) {
            str = "ncd-ncj+";
        }
        String strmonth = "";
        int flag = 0;
        if (isEmpty(endmonth)) {
            flag = Integer.parseInt(endmonth);
        }
        if (flag != 0) {
            for (int i = 1; i <= flag; i++) {
                str += first + i + "-" + second + i + "+";
            }
        }

        if (isEmpty(str)) {
            strmonth = str.substring(0, str.length() - 1);
        }
        return strmonth;

    }

    /**
     * 处理多属性
     */
    public static List<String> treatStr(String... strs) {
        List newstrs = new ArrayList();
        for (String str : strs) {
            List list = transforStr(str);
            if (list != null && list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    newstrs.add(list.get(i));
                }
            }
        }
        return newstrs;
    }

    /**
     * [95211009029004||政府绩效管理工作][95211010902017||公务员培训][95220401900104||国际组织会费]
     * 变成95211009029004 95211010902017 95220401900104
     */
    public static List<String> changeForProj(String str) {
        List<String> strs = new ArrayList();
        if (str.contains("]")) {
            String[] srs = str.split("]");
            System.out.println(srs.length);
            for (String stri : srs) {
                if (stri.contains("[") && stri.contains("||")) {
                    String str1 = stri.substring(stri.indexOf("[") + 1, stri.indexOf("||"));
//                    System.out.println(str1);
                    strs.add(str1);
                }
            }

        }
        return strs;

    }

    /**
     * 得到月份 '201001' --->'1'
     */
    public static String getMonth(String mth) {
        if (mth != null && mth.length() == 6) {

            if (mth.substring(4, 5).equals("0")) {
                return mth.substring(5, 6);
            }
            return mth.substring(4, 6);
        }
        return "";
    }

    /**
     * 得到年份 '201001'--->'2010'
     */
    public static String getYear(String mth) {

        if (mth != null && mth.length() == 6) {
            return mth.substring(0, 4);
        }
        return "";
    }

    /**
     * if(list!=null && list.size()>0){
     * return true;
     * }
     */
    public static boolean listEmpty(List list) {
        if (list != null && list.size() > 0) {
            return true;
        }
        return false;
    }

    /**
     * 将list变成'2121','2121'
     */
    public static String tranList(List<String> list) {
        String flag = "";
        if (list != null && list.size() > 0) {
            for (String str : list) {
                if (isEmpty(str)) {
                    if (str.contains(",")) {
                        String[] strl = str.split(",");
                        for (String stri : strl) {
                            if (isEmpty(stri)) {
                                flag += "'" + stri.trim() + "',";
                            }
                        }
                    } else {
                        flag += "'" + str.trim() + "',";
                    }
                }
            }
        }
        if (isEmpty(flag)) {
            flag = flag.substring(0, flag.length() - 1);
        }else{
            flag = "''";
        }
        return flag;
    }

    /**
     * 处理路径
     * uploadResources/91968a4d-87ca-4efa-a9d5-8bf4fba7823e/1214.sql 变成 1214.sql
     */
    public static String getShortPath(String path) {
        if (path != null && path.contains("/")) {
            String pathname = path.substring(path.lastIndexOf("/")+1, path.length());
            return pathname;
        }
        return "";
    }
    /**
     * 处理路径  windows 本地路径
     * uploadResources/91968a4d-87ca-4efa-a9d5-8bf4fba7823e/1214.sql 变成 1214.sql
     */
    public static String getShortPathWin(String path) {
        if (path != null && path.contains("\\")) {
            String pathname = path.substring(path.lastIndexOf("\\")+1, path.length());
            return pathname;
        }
        return "";
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
//        System.out.println(getShortPath("uploadResources/91968a4d-87ca-4efa-a9d5-8bf4fba7823e/1214.sql "));
//        changeForProj("[95211009029004||政府绩效管理工作][95211010902017||公务员培训][95220401900104||国际组织会费]");
//        System.out.println(matchMonthString("yj", "yd", "5"));
//        System.out.println(matchMonthString("yj"));
//        transforStr("[028002]一般行政管理事务现金,[051002]一般行政管理事务限额");
//        String str = "                 ";
        // System.out.println(trim(str));

        // System.out.println(getSqlString(""));
        ;


//        System.out.print(getSqlString(""));

//        String[] strs=new String[]{"  aa  ","bb"};
//
//        String[] strb=new String[]{"aa","  bb  "};
//           // System.out.println(compareArray(strs,strb)+"---------------");
        //// System.out.println(gettimes("afsflsdf123456", "23"));

//		String[] strs=dealString("1|�칫��","|");
//		String[] str1s="1-�칫��".split("-");
//		// System.out.println(str1s.length);
//		for (String string : str1s) {
//			// System.out.println(string);
//		}

        String str1 = "                ";
        String str2 = "1";
        String str3 = "1";
//        //// System.out.println(isAllEmpty(str1,str2,str3));
         System.out.println(isEmpty(str1, str2, str3));
    }

     public static String tranListForLike(List<String> list) {
        String flag = "";
         if (list != null && list.size() > 0) {
            for (String str : list) {
                if (isEmpty(str)) {
                    if (str.contains(",")) {
                        String[] strl = str.split(",");
                        for (String stri : strl) {
                            if (isEmpty(stri)) {
                                flag += " kmdm like '" + stri.trim() + "%' or";
                            }
                        }
                    } else {
                        flag += " kmdm like '" + str.trim() + "%' or";
                    }
                }
            }
        }
        if (isEmpty(flag)) {
            flag = flag.substring(0, flag.length() - 3);
        }else{
            flag = "kmdm = ''";
        }
        return flag;
    }
     //3333,2222
      public static String getSqlStringForLike(String str) {
        String string = "";
        if (isEmpty(str)) {
            if (str.contains(",")) {
                String[] strs = str.split(",");
                for (int i = 0; i < strs.length; i++) {
                    if (i == strs.length - 1) {
                        string += " kmdm like '" + strs[i].trim() + "%'";
                    } else {
                        string += " kmdm like '" + strs[i].trim() + "%' or";
                    }
                }
            }
             else {
                string = " kmdm like '" + str.trim() + "%'";
            }
        } else {
            string = "kmdm = ''";
        }
        return string;
    }
      
//     /**
//     * 将list变成'2121','2121'
//     */
//    public static String fzdmList(List<FunProjKmModel> list) {
//        String flag = "";
//        if (!Null.check(list)) {
//            for (FunProjKmModel str : list) {
//                flag += str.getJzcg().trim()+","+ str.getBmjzcg().trim()+"," + str.getFscg().trim() + "," + str.getWzfcg().trim() + ",";
//            }
//        }
//        if(!Null.check(flag)){
//            flag = flag.substring(0, flag.length() - 1);
//        }else{
//            flag = "''";
//        }
//        return flag;
//    }
      
//    
//    public static String fzdmGovList(List<FunProjKmModel> list) {
//        String flag = "";
//        if (!Null.check(list)) {
//            for (FunProjKmModel str : list) {
//                flag += str.getJzcg().trim()+","+ str.getBmjzcg().trim()+"," + str.getFscg().trim() + ",";
//            }
//        }
//        if(!Null.check(flag)){
//            flag = flag.substring(0, flag.length() - 1);
//        }else{
//            flag = "''";
//        }
//        return flag;
//    }
}
